package com.haole.ecommercepay.ecommerce.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haole.ecommercepay.common.HttpUtils;
import com.haole.ecommercepay.ecommerce.entity.applyments.MerchantApplyParam;
import com.haole.ecommercepay.ecommerce.entity.applyquery.ApplyQueryParam;
import com.haole.ecommercepay.ecommerce.service.EcommerceService;
import com.haole.ecommercepay.ecommerce.url.EcommerceUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author haole
 * 商户进件 https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/ecommerce.shtml
 */
@Service("ecommerceService")
public class EcommerceServiceImpl implements EcommerceService {

    private static final Logger logger = LoggerFactory.getLogger(EcommerceServiceImpl.class);

    /**
     * 二级商户进件
     * @param param
     * @return
     */
    @Override
    public JSONObject applyments(MerchantApplyParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("ecommerce applyments param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", EcommerceUrl.APPLYMENTS,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("ecommerce applyments result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 通过申请单ID查询申请状态
     * @param param
     * @return
     */
    @Override
    public JSONObject applymentsId(ApplyQueryParam param) {
        logger.info("ecommerce applymentsId param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(EcommerceUrl.APPLYMENT_ID,param.getQueryId()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("ecommerce applymentsId result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 通过业务申请编号查询申请状态
     * @param param
     * @return
     */
    @Override
    public JSONObject outRequestNo(ApplyQueryParam param) {
        logger.info("ecommerce outRequestNo param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(EcommerceUrl.OUT_REQUEST_NO,param.getQueryId()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("ecommerce outRequestNo result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 下载平台证书
     * @return
     */
    @Override
    public JSONObject certificates() {
        return null;
    }
    /**
     * 修改结算帐号
     * @return
     */
    @Override
    public JSONObject modifySettlement() {
        return null;
    }
    /**
     * 查询结算账户
     * @return
     */
    @Override
    public JSONObject settlement() {
        return null;
    }
}
